package com.socialuni.sdk.websocket;


import com.socialuni.sdk.model.DO.user.UserDO;
import com.socialuni.sdk.utils.CommonTokenUtil;
import com.socialuni.sdk.utils.UserUtils;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.support.DefaultHandshakeHandler;

import java.security.Principal;
import java.util.Map;

public class CustomHandshakeHandler extends DefaultHandshakeHandler {
    @Override
    protected Principal determineUser(ServerHttpRequest request,
                                      WebSocketHandler wsHandler,
                                      Map<String, Object> attributes) {
        //websocket下就从req中拿token
        //获取token认证
        String token = CommonTokenUtil.getToken();
//        Integer devId = DevAccountUtils.getDevId();
        //32为非法token，但是在这里是正常token，代表临时用户
        //如果为32位token则在这里可以直接使用
        if (CommonTokenUtil.isSuccess(token)) {
            UserDO user = UserUtils.getUserByWebsocketToken(token);
            return new WebSocketUser(user.getId().toString());
        } else {
            return new WebSocketUser(token);
        }
    }
}